package ecweb.ui.account;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecweb.domain.User;
import ecweb.exception.DatabaseException;
import ecweb.exception.ItemAlreadyExistsException;
import ecweb.service.IUserService;
import ecweb.service.impl.UserService;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/Account/Register")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private IUserService userService = new UserService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      request.getRequestDispatcher("register.jsp").forward(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if ("register".equals(action)) {
			doRegister(request, response);
		} else {
			response.sendError(403);
		}
	}
	
	private void doRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realName = request.getParameter("realName");
		String phoneNumber = request.getParameter("phoneNumber");
		String address = request.getParameter("address");

		if (username == null || "".equals(username) ||
				password == null || "".equals(password) ||
				realName == null || "".equals(realName) ||
				phoneNumber == null || "".equals(phoneNumber) ||
				address == null || "".equals(address)
				) {
			response.sendError(403);
			return;
		}
		
		if (username.length() < 5 || password.length() < 6) {
			response.sendError(403);
			return;
		}
		
		int errCode = 0;
		User user = null;
		try {
			user = userService.register(username, password, realName, phoneNumber, address);
		} catch (ItemAlreadyExistsException e) {
			errCode = 1;
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		
		if (errCode == 0) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			response.sendRedirect(request.getContextPath() + "/");
			return;
		}	
		
		request.setAttribute("errCode", errCode);
		request.getRequestDispatcher("register.jsp").forward(request, response);
	}
}
